package com.hxd.otc.module.convenience.fragment;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseApplication;
import com.hxd.otc.base.BaseRecyclerFragment;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.common.enums.ConvenienceBusinessTypeEnum;
import com.hxd.otc.module.convenience.activity.ConvenienceOrderActivity;
import com.hxd.otc.module.convenience.activity.HydropowerListActivity;
import com.hxd.otc.module.convenience.activity.OilListActivity;
import com.hxd.otc.module.convenience.activity.PrepaidRechargeActivity;
import com.hxd.otc.module.convenience.activity.VideoServiceRechargeActivity;
import com.hxd.otc.module.convenience.adapter.ConvenienceAdapter;
import com.hxd.otc.module.convenience.adapter.ConvenienceBannerAdapter;
import com.hxd.otc.module.convenience.bean.ConvenienceBannerBean;
import com.hxd.otc.module.convenience.bean.RefuelingServiceBean;
import com.hxd.otc.module.convenience.contract.ConvenienceContract;
import com.hxd.otc.module.convenience.presenter.ConveniencePresenter;
import com.hxd.otc.module.login.activity.LoginActivity;
import com.hxd.otc.module.my.activity.WebViewActivity;
import com.hxd.otc.utils.DataUtils;
import com.hxd.otc.widget.TitleBar;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.Util;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by lzy on 2018/12/14 0014.
 * description ${便民服务}
 */
public class ConvenienceFragment extends BaseRecyclerFragment<ConveniencePresenter, ConvenienceAdapter, RefuelingServiceBean> implements ConvenienceContract.IView {

    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.viewPager)
    RollPagerView mViewPager;
    private ConvenienceBannerAdapter mBannerAdapter;

    public static final String EXTRA_REFUELING_SERVICE_BEAN = "refueling_service_bean";
    public static final String EXTRA_BUSINESS_TYPE = "business_type";
    private ColorPointHintView mHintView;

    @Override
    protected ConvenienceAdapter createAdapter() {
        return new ConvenienceAdapter(getContext());
    }

    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return new GridLayoutManager(getContext(), 2);
    }

    @Override
    protected ConveniencePresenter createPresenter() {
        return new ConveniencePresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_convenience;
    }

    @Override
    protected void initView() {
        super.initView();

        int width = DataUtils.getScreenWidth(BaseApplication.getInstance()) - DataUtils.dp2px(BaseApplication.getInstance(), 40);
        int height = (int) (width * 99f / 335f);
        ViewGroup.LayoutParams layoutParams = mViewPager.getLayoutParams();
        layoutParams.height = height;
        mViewPager.setLayoutParams(layoutParams);
        mViewPager.setAnimationDurtion(1000);
        mViewPager.setPlayDelay(3000);
        mViewPager.setAnimationDurtion(1000);
        mBannerAdapter = new ConvenienceBannerAdapter(mViewPager);
        mViewPager.setAdapter(mBannerAdapter);
        mViewPager.setHintView(null);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        titleBar.setOnTitleBarItemClickListener(new TitleBar.OnTitleBarItemClickListener() {
            @Override
            public void onBack(View v) {

            }

            @Override
            public void onRight(View v) {
                if (!UserHelper.isLogin()) {
                    $startActivity(LoginActivity.class);
                    return;
                }
                $startActivity(ConvenienceOrderActivity.class);
            }
        });
        mBannerAdapter.setOnBannerClickListener(new ConvenienceBannerAdapter.OnBannerClickListener() {
            @Override
            public void onBannerClick(ConvenienceBannerBean convenienceBannerBean) {
                String bannerLink = convenienceBannerBean.getBannerLink();
                if (!TextUtils.isEmpty(bannerLink)) {
                    mPresenter.bannerUpdate(convenienceBannerBean.getId());
                    Bundle bundle = new Bundle();
                    bundle.putString(WebViewActivity.EXTRA_TITLE, convenienceBannerBean.getBannerName());
                    bundle.putString(WebViewActivity.EXTRA_URL, bannerLink);
                    $startActivity(WebViewActivity.class, bundle);
                }
            }
        });
    }

    @Override
    public void onItemClick(int position) {
        super.onItemClick(position);
        RefuelingServiceBean item = mAdapter.getItem(position);
        ConvenienceBusinessTypeEnum businessTypeEnum = ConvenienceBusinessTypeEnum.getInstance(item.getBusinessType());
        Bundle bundle = new Bundle();
        bundle.putSerializable(EXTRA_REFUELING_SERVICE_BEAN, item);
        bundle.putSerializable(EXTRA_BUSINESS_TYPE, businessTypeEnum);
        switch (businessTypeEnum) {
            case LIVING://生活服务
                if (UserHelper.isLogin()) {
                    $startActivity(HydropowerListActivity.class, bundle);
                } else {
                    $startActivity(LoginActivity.class);
                }
                break;
            case PHONE://手机缴费
                $startActivity(PrepaidRechargeActivity.class, bundle);
                break;
            case GAS://加油卡充值
                $startActivity(OilListActivity.class, bundle);
                break;
            case VIDEO://视频充值
                $startActivity(VideoServiceRechargeActivity.class, bundle);
                break;
            case OTHER:
                showToast(String.format(getString(R.string.this_feature_sub), item.getStatusEnum().getDesc()));
                break;
        }
    }

    @Override
    public void getBannerListSuccess(ArrayList<ConvenienceBannerBean> convenienceBannerBeans) {
        mBannerAdapter.setNewData(convenienceBannerBeans);
        updateBannerPlay();
    }

    private void updateBannerPlay() {
        if (mViewPager == null) return;
        if (mBannerAdapter == null) return;
        int realCount = mBannerAdapter.getRealCount();
        if (realCount > 1 && isAdded() && getUserVisibleHint()) {
            mViewPager.resume();
            mViewPager.setEnabled(true);
            mViewPager.setHintView(getHintView());
            mViewPager.setHintPadding(DensityUtil.dp2px(3), DensityUtil.dp2px(6), DensityUtil.dp2px(3), DensityUtil.dp2px(6));
        } else {
            mViewPager.pause();
            mViewPager.setEnabled(false);
            mViewPager.setHintView(null);
        }
    }

    public ColorPointHintView getHintView() {
        if (mHintView == null) {
            mHintView = new ColorPointHintView(mContext, mContext.getResources().getColor(android.R.color.white),
                    mContext.getResources().getColor(R.color.color_white_30)) {
                @Override
                public Drawable makeFocusDrawable() {
                    GradientDrawable dot_focus = new GradientDrawable();
                    dot_focus.setColor(mContext.getResources().getColor(android.R.color.white));
                    dot_focus.setCornerRadius(Util.dip2px(getContext(), 2f));
                    dot_focus.setSize(Util.dip2px(getContext(), 4), Util.dip2px(getContext(), 4));
                    return dot_focus;
                }

                @Override
                public Drawable makeNormalDrawable() {
                    GradientDrawable dot_normal = new GradientDrawable();
                    dot_normal.setColor(mContext.getResources().getColor(R.color.color_white_30));
                    dot_normal.setCornerRadius(Util.dip2px(getContext(), 2f));
                    dot_normal.setSize(Util.dip2px(getContext(), 4), Util.dip2px(getContext(), 4));
                    return dot_normal;
                }
            };
        }
        return mHintView;
    }

    @Override
    public void getBannerListFail(String errMessage) {
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        super.onRefresh(refreshLayout);
        if (mBannerAdapter.getRealCount() == 0 && mPresenter != null) {
            mPresenter.getBannerList();
        }
    }

    @Override
    protected void lazyLoad() {
        super.lazyLoad();
        updateBannerPlay();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mViewPager != null) mViewPager.pause();
    }

    @Override
    public void onResume() {
        super.onResume();
        updateBannerPlay();
    }

    @Override
    public void onPause() {
        super.onPause();
        updateBannerPlay();
    }
}
